package com.runchinaup.download.okhttp;

import android.text.TextUtils;
import android.util.Log;

import com.runchinaup.download.App;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Okhttp {

    private volatile static Okhttp mInstance;

    private OkHttpClient okHttpClient;

    public static Okhttp getInstance() {
        if (mInstance == null) {
            synchronized (Okhttp.class) {
                if (mInstance == null) {
                    mInstance = new Okhttp();
                }
            }
        }
        return mInstance;
    }


    private Okhttp() {
        File sdcache = App.getApp().getExternalCacheDir();
        int cacheSize = 10 * 1024 * 1024;
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize));
        okHttpClient = builder.build();
    }

    /**
     * 异步get请求
     *
     * @param url
     * @param callback
     */

    public void getAsynHttp(String url, final ResultCallback callback) {
        okHttpClient = new OkHttpClient();
        Request.Builder builder = new Request.Builder().url(url);
        //可以省略，默认是GET请求
        builder.method("GET", null);
        Request request = builder.build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callback.onError(call, e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callback.onResponse(response);
            }
        });
    }

    /**
     * 异步post请求
     *
     * @param url
     * @param callback
     * @param params
     */

    public void postAsynHttp(String url, final ResultCallback callback, Param[] params) {
        okHttpClient = new OkHttpClient();
        if (params == null) {
            params = new Param[0];
        }
        FormBody.Builder formBody = new FormBody.Builder();
        for (Param param : params) {
            if (param != null && !TextUtils.isEmpty(param.name) && !TextUtils.isEmpty(param.value)) {
                formBody.add(param.name, param.value);
                Log.e("postAsynHttp: ", param.name + "==>" + param.value);
            }
        }
        RequestBody requestBody = formBody.build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callback.onError(call, e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callback.onResponse(response);
            }
        });

    }

    public static class Param {

        public String name;
        public String value;

        public Param() {
        }

        public Param(String name, String value) {
            this.name = name;
            this.value = value;
        }

    }

}
